//
//  HJDNameViewController.m
//  JYHospital
//
//  Created by 胡金东 on 16/6/15.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDNameViewController.h"
#import "AFNetworking.h"
@interface HJDNameViewController ()
@property (nonatomic,strong)  UITextField *text ;


@end

@implementation HJDNameViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor getColorWithHexNumber:@"CFCFCF"];
    self.title = @"名字";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
      NSForegroundColorAttributeName:[UIColor getColorWithHexNumber:@"848484"]}];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor getColorWithHexNumber:@"848484"] forState:UIControlStateNormal];
    [leftBtn sizeToFit];
    [leftBtn addTarget:self action:@selector(backItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
        self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor getColorWithHexNumber:@"5CC6AB"] forState:UIControlStateNormal];
    [rightBtn sizeToFit];
    [rightBtn addTarget:self action:@selector(saveItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;

    [self setUpText];

    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    
}

//隐藏键盘
-(void)viewTapped:(UITapGestureRecognizer*)tap
{
    
    [self.view endEditing:YES];
    
}


-(void)backItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setUpText{
    
            NSData *nameDa = [[NSUserDefaults standardUserDefaults]valueForKey:@"nameD"];
            NSString *name = [NSKeyedUnarchiver unarchiveObjectWithData:nameDa];
   
    
    self.text = [[UITextField alloc]initWithFrame:CGRectMake(0, HJDVerticalMargin + HJDNavigHeight, HJDWidth, 50)];
    self.text.backgroundColor = [UIColor whiteColor];
    if (![name isKindOfClass:[NSNull class]]) {
          self.text.text = name;
    }
    
    self.text.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.text];
}


-(void)saveItemClick{
    
    if (self.text.text.length >0 ) {
        [self requestNickName];
    }
    if (self.text.text.length == 0) {
        [MBProgressHUD showError:@"名字不能为空"];
    }

    
    
}

-(void)requestNickName{
    
    NSString *url = @"/app/modifyUser.do";
    NSString *path = [Hurl stringByAppendingString:url];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *phone = [defaults objectForKey:@"phonenum"];
    NSString *pregnancy = [defaults objectForKey:@"pregnanc"];

    NSString *nickname = [defaults objectForKey:@"changNick"];
    NSData *qqDa = [[NSUserDefaults standardUserDefaults]valueForKey:@"qqD"];
    NSString *qq = [NSKeyedUnarchiver unarchiveObjectWithData:qqDa];
    
    
    NSData *dueDa = [[NSUserDefaults standardUserDefaults]valueForKey:@"due"];
    NSString *dueDate = [NSKeyedUnarchiver unarchiveObjectWithData:dueDa];
    

    params[@"phone"] = phone;
    params[@"nickName"] = nickname;
    params[@"name"] = self.text.text;
    params[@"qq"] = qq;
    params[@"pregnancy"] = pregnancy;
    params[@"dueDate"] = dueDate;
    
    [mgr POST:path parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSData *nameDe = [NSKeyedArchiver archivedDataWithRootObject:self.text.text];
        [[NSUserDefaults standardUserDefaults]setValue:nameDe forKey:@"nameD"];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD showError:@"服务器升级，请稍后再试"];

    }];
    
   
}

@end
